package com.sskj.b70.v.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.b70.R;
import com.sskj.b70.common.App;
import com.sskj.b70.common.MoneyValueFilter;
import com.sskj.b70.common.adapter.BaseAdapter;
import com.sskj.b70.common.adapter.ViewHolder;
import com.sskj.b70.m.bean.PayWayBean;
import com.sskj.b70.m.bean.TabEntity;
import com.sskj.b70.p.activity.FabiPublishPresenter;
import com.sskj.b70.utils.ClickUtil;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.base.BaseActivity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;

/**
 * C2C交易---发布
 */
public class FabiPublishActivity extends BaseActivity<FabiPublishPresenter> {
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_right)     // 顶部右侧ImageView
            ImageView ivRight;
    @BindView(R.id.pay_way_recyclerview)
    RecyclerView mPayWayRecyclerview;   // 支付方式下的RecyclerView
    @BindView(R.id.payType)
    TextView payType;  // 支付方式字段
    @BindView(R.id.fabi_publish_count)
    EditText mFabiPublishCount;
    @BindView(R.id.fabi_publish_input_low)
    EditText mFabiPublishInputLow;
    @BindView(R.id.fabi_publish_input_hight)
    EditText mFabiPublishInputHight;
    @BindView(R.id.fabi_publish_input_unit_price)
    EditText mFabiPublishInputUnitPrice;
    @BindView(R.id.fabi_publish_input_remark)
    EditText mFabiPublishInputRemark;
    @BindView(R.id.bt_control)
    Button mBtControl;   // 确认按钮
    @BindView(R.id.tvTip)
    TextView tvTip;   // 确认按钮
    private String[] mTitles = {App.INSTANCE.getString(R.string.strFabiPublishActivity0), App.INSTANCE.getString(R.string.strFabiPublishActivity1)};
    private BaseAdapter<PayWayBean> mBaseAdapter;
    /**
     * 发布类型 0：出售，  or 求购
     */
    private int mIndex = 0;
    private int mPayWx;
    private int mPayAli;
    private int mPayBank;
    private View mFooterView;
    private View mFailView;
    private View mStartLading;
    private View mLoadingProgress;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_fabi_publish;
    }

    @Override
    public FabiPublishPresenter getPresenter() {
        return new FabiPublishPresenter();
    }

    @Override
    protected void initView() {
        mPresenter.requestUnitPrice();
        ClickUtil.click(ivBack, this::finish);
        ClickUtil.click(ivRight, () -> {
            FabiSellListActivity.start(this, mIndex == 1);
        });
        ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mIndex = position;
                mBtControl.setText(position == 0 ? App.INSTANCE.getString(R.string.strFabiPublishActivity2) : App.INSTANCE.getString(R.string.strFabiPublishActivity3));
                if (position == 0) {
                    payType.setVisibility(View.VISIBLE);
                    tvTip.setText(App.INSTANCE.getString(R.string.strFabiPublishActivity4));
                    mPayWayRecyclerview.setVisibility(View.VISIBLE);
                } else {
                    tvTip.setText(App.INSTANCE.getString(R.string.strFabiPublishActivity5));
                    payType.setVisibility(View.GONE);
                    mPayWayRecyclerview.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        mFabiPublishInputHight.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(2)});
        mFabiPublishInputLow.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(2)});
        mFabiPublishInputUnitPrice.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(2)});
        mFabiPublishCount.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(4)});

        mPayWayRecyclerview.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        mPayWayRecyclerview.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mBaseAdapter = new BaseAdapter<PayWayBean>(R.layout.recy_item_pay_way, null, mPayWayRecyclerview) {

            @Override
            public void bind(ViewHolder holder, PayWayBean item) {
//                holder.setText(R.id.item_checkbox, item.getTip());
                if ("1".equals(item.getType())) {
                    ((CheckBox) holder.getView(R.id.item_checkbox)).setText(App.INSTANCE.getString(R.string.strFabiPublishActivity7));
                    Drawable drawable1 = getResources().getDrawable(R.drawable.selector_checkbox);
                    drawable1.setBounds(0, 0, 30, 30);
                    Drawable drawable2 = getResources().getDrawable(R.mipmap.icon_wx);
                    drawable1.setBounds(0, 0, 30, 30);

//                    ((CheckBox) holder.getView(R.id.item_checkbox)).setCompoundDrawables(drawable2, null, drawable1, null);
                    ((CheckBox) holder.getView(R.id.item_checkbox)).setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_wx, 0, R.drawable.selector_checkbox, 0);

                } else if ("2".equals(item.getType())) {
                    ((CheckBox) holder.getView(R.id.item_checkbox)).setText(App.INSTANCE.getString(R.string.strFabiPublishActivity9));
                    Drawable drawable1 = getResources().getDrawable(R.drawable.selector_checkbox);
                    drawable1.setBounds(0, 0, 30, 30);
                    Drawable drawable2 = getResources().getDrawable(R.mipmap.icon_ali);
                    drawable1.setBounds(0, 0, 30, 30);

//                    ((CheckBox) holder.getView(R.id.item_checkbox)).setCompoundDrawables(drawable2, null, drawable1, null);
                    ((CheckBox) holder.getView(R.id.item_checkbox)).setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_ali, 0, R.drawable.selector_checkbox, 0);
                } else if ("3".equals(item.getType())) {
                    ((CheckBox) holder.getView(R.id.item_checkbox)).setText(App.INSTANCE.getString(R.string.strFabiPublishActivity11));
                    Drawable drawable1 = getResources().getDrawable(R.drawable.selector_checkbox);
                    drawable1.setBounds(0, 0, 30, 30);
                    Drawable drawable2 = getResources().getDrawable(R.mipmap.icon_bank);
                    drawable1.setBounds(0, 0, 30, 30);

//                    ((CheckBox) holder.getView(R.id.item_checkbox)).setCompoundDrawables(drawable2, null, drawable1, null);

                    ((CheckBox) holder.getView(R.id.item_checkbox)).setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_bank, 0, R.drawable.selector_checkbox, 0);
                }
                holder.setOnCheckedChangeListener(R.id.item_checkbox, (buttonView, isChecked) -> {
                    if (isChecked) {
                        if ("1".equals(item.getType())) {
                            mPayWx = 1;
                        } else if ("2".equals(item.getType())) {
                            mPayAli = 1;
                        } else if ("3".equals(item.getType())) {
                            mPayBank = 1;
                        }
                    } else {
                        if ("1".equals(item.getType())) {
                            mPayWx = 0;
                        } else if ("2".equals(item.getType())) {
                            mPayAli = 0;
                        } else if ("3".equals(item.getType())) {
                            mPayBank = 0;
                        }
                    }
                });
            }
        };
        mFooterView = LayoutInflater.from(this).inflate(R.layout.item_text, null);
        mFooterView.setOnClickListener(v -> startActivity(new Intent(mActivity, SecurityCenterActivity.class)));
        mFailView = LayoutInflater.from(this).inflate(R.layout.layout_reset_loading, null);
        mStartLading = mFailView.findViewById(R.id.start_loading);
        mLoadingProgress = mFailView.findViewById(R.id.loading_progress);
        mStartLading.setOnClickListener(v -> {
            mStartLading.setVisibility(View.INVISIBLE);
            mLoadingProgress.setVisibility(View.VISIBLE);
            mPresenter.requsetPayWayList();
        });


        mBtControl.setOnClickListener(v -> {
            String sumCount = mFabiPublishCount.getText().toString().trim();
            String lowPrice = mFabiPublishInputLow.getText().toString().trim();
            String hightPrice = mFabiPublishInputHight.getText().toString().trim();
            String unitPrice = mFabiPublishInputUnitPrice.getText().toString().trim();

            if (TextUtils.isEmpty(sumCount)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiPublishActivity18));
                return;
            } else if (TextUtils.isEmpty(lowPrice)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiPublishActivity19));
                return;
            } else if (TextUtils.isEmpty(hightPrice)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiPublishActivity20));
                return;
            } else if (TextUtils.isEmpty(unitPrice)) {
//                mPresenter.requestUnitPrice();
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiPublishActivity21));
                return;
            } else if (Double.valueOf(unitPrice) == 0) {
//                mPresenter.requestUnitPrice();
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiPublishActivity22));
                return;
            } else if (mIndex == 0 && mPayWx + mPayAli + mPayBank == 0) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiPublishActivity23));
                return;
            }

            Double lowPriceD = Double.valueOf(lowPrice);
            Double hightPriceD = Double.valueOf(hightPrice);
            Double sumCountD = Double.valueOf(sumCount);
            Double unitPriceD = Double.valueOf(unitPrice);

            if (lowPriceD >= hightPriceD) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiPublishActivity24));
                return;
            } else if (lowPriceD < unitPriceD) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiPublishActivity25));
                return;
            } else if (lowPriceD > unitPriceD * sumCountD) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiPublishActivity26));
                return;
            }

            String remark = mFabiPublishInputRemark.getText().toString().trim();
//            if (TextUtils.isEmpty(remark)) {
//                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiPublishActivity27));
//                return;
//            }


            mPresenter.requsetPublish(mIndex == 0 ? "2" : "1", sumCount, lowPrice, hightPrice, unitPrice, mPayWx, mPayAli, mPayBank, remark);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.requsetPayWayList();
//        mPresenter.requestUnitPrice();
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, FabiPublishActivity.class);
        context.startActivity(starter);
    }

    public void onHttpFail() {
        mBaseAdapter.removeFooterView(mFooterView);
        mBaseAdapter.setHeaderView(mFailView);
        mStartLading.setVisibility(View.VISIBLE);
        mLoadingProgress.setVisibility(View.INVISIBLE);
    }

    public void onPayWaySuccess(List<PayWayBean> list) {
        mBaseAdapter.removeHeaderView(mFailView);
        //使用java.util.Iterator
        for (Iterator it = list.iterator(); it.hasNext(); ) {
            PayWayBean obj = (PayWayBean) it.next();
            if (obj.isDisable()) {
                it.remove();
            }
        }
        if (list.size() < 3) {
            if (mBaseAdapter.getFooterLayoutCount() == 0) {
                mBaseAdapter.addFooterView(mFooterView);
            }
        }
        mBaseAdapter.setNewData(list);
    }

    public void onPublishPriceSuccess(String price) {
        mFabiPublishInputUnitPrice.setText(price);
    }
}
